package cn.iocoder.yudao.module.signature.dal.mysql.signinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.signature.controller.app.signinfo.vo.ApplicationSignInfoPageReqVO;
import cn.iocoder.yudao.module.signature.dal.dataobject.signinfo.ApplicationSignInfoDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 应用签名记录 Mapper
 *
 * @author base
 */
@Mapper
public interface ApplicationSignInfoMapper extends BaseMapperX<ApplicationSignInfoDO> {

    IPage<ApplicationSignInfoDO> list(Page<?> page, @Param("reqVo") ApplicationSignInfoPageReqVO reqVO);
    default PageResult<ApplicationSignInfoDO> selectPage(ApplicationSignInfoPageReqVO reqVO) {

        LambdaQueryWrapperX<ApplicationSignInfoDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<ApplicationSignInfoDO>()
                .eqIfPresent(ApplicationSignInfoDO::getApplicationId, reqVO.getApplicationId())
                .eqIfPresent(ApplicationSignInfoDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ApplicationSignInfoDO::getUdid, reqVO.getUdid())
                .eqIfPresent(ApplicationSignInfoDO::getStatus, reqVO.getStatus())
                .likeIfPresent(ApplicationSignInfoDO::getPacketName, reqVO.getPacketName())
                .likeIfPresent(ApplicationSignInfoDO::getApplicationName, reqVO.getApplicationName())
                .betweenIfPresent(ApplicationSignInfoDO::getCreateTime, reqVO.getCreateTime());

        if (StringUtils.isNotBlank(reqVO.getQryParam())) {
            lambdaQueryWrapperX.and(wrapper ->
                    wrapper.like(ApplicationSignInfoDO::getUdid, reqVO.getQryParam())
                            .or().like(ApplicationSignInfoDO::getPacketName, reqVO.getQryParam())
                            .or().like(ApplicationSignInfoDO::getApplicationName, reqVO.getQryParam()));
        }
        lambdaQueryWrapperX.orderByDesc(ApplicationSignInfoDO::getId);
        return selectPage(reqVO, lambdaQueryWrapperX);
    }


    default List<ApplicationSignInfoDO>  listRecentData(Long userId){
        ApplicationSignInfoDO applicationSignInfoDO = selectOne(new LambdaQueryWrapperX<ApplicationSignInfoDO>()
                .eq(ApplicationSignInfoDO::getUserId,userId)
                .orderByDesc(ApplicationSignInfoDO::getId)
                .last("limit 1 "));
        if(applicationSignInfoDO == null){
            return new ArrayList<>();
        }
//        LocalDateTime createTime = applicationSignInfoDO.getCreateTime();
//        LocalDateTime fiveMinutesAgo = createTime.minusMinutes(5);
        Date date = DateUtils.addMinutes(new Date(), -5);
        return selectList(new LambdaQueryWrapperX<ApplicationSignInfoDO>()
                .eq(ApplicationSignInfoDO::getUserId,userId)
                .eq(ApplicationSignInfoDO::getUdid,applicationSignInfoDO.getUdid())
                .ge(ApplicationSignInfoDO::getCreateTime,date)
                .orderByDesc(ApplicationSignInfoDO::getId));
    }

}